UpgradeManager = {}
--UpgradeManagerFunc = {}
--UpgradeManagerFunc.__index = UpgradeManagerFunc
--setmetatable(UpgradeManager, UpgradeManagerFunc)
UpgradeManager.upgradeBuilds = {}--Array:create() 
UpgradeManager.buildingScenes = {[1001]=ZhiHuiBuScene, [1003]=KeXueYuanScene, [1004]=ZhanFuYingScene, [1005]=XunLianYingScene, [1006]=YinHangScene, [1007]=JunYingScene}
UpgradeManager.hasTimer = false

UpgradeManager.upgradeBuilding = function(bid)
  print("UpgradeManager.upgradeBuilding", bid)
  
  local bl = UserData.userinfo.host.cities2[tostring(bid)].l
  local binfo = UserData.config.models[BuildingLevel[bid]][bl]
  TimeManager.saveCDTime(string.format("upcd%d", bid), binfo.coolingTime)
  UpgradeManager.buildingScenes[bid].upgradeCooldown = binfo.coolingTime
  UpgradeManager.checkUpgrade(bid)
  --UpgradeManager.setUpgradeCoolDown(bid)
  --if UpgradeManager.hasTimer==false then
  --  print("UpgradeManager.upgradeBuilding start timer", binfo.coolingTime)
  --  UpgradeManager.upgradeTimerID = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(onUpgradeTimer, 1, false)
  --  UpgradeManager.hasTimer = true
  --end

  local sc = UpgradeManager.buildingScenes[bid]
  if sc.instance~=nil then
    print("UpgradeManager.upgradeBuilding update", sc.instance, bid)
    UserData.userinfo.host.cities2[tostring(bid)].l = bl+1
    setBuildingBaseData(bid, sc.instance.node)
  end
end
UpgradeManager.checkUpgrade = function(bid)
  local sc = UpgradeManager.buildingScenes[bid]
  if sc.upgradeCooldown==nil then
    sc.upgradeCooldown = TimeManager.getLeftDuration(string.format("upcd%d", bid))
  end
  UpgradeManager.setUpgradeCoolDown(bid)
  if sc.upgradeCooldown>0 then
    --UpgradeManager.upgradeBuilds:append(bid)
    UpgradeManager.upgradeBuilds[bid] = 0
    if UpgradeManager.upgradeTimerID==nil then
      UpgradeManager.upgradeTimerID = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(onUpgradeTimer, 1, false)
    end
  end
end
UpgradeManager.setUpgradeCoolDown = function(bid)
  local sc = UpgradeManager.buildingScenes[bid]
  --if sc.upgradeCooldown==nil then
  --  sc.upgradeCooldown = TimeManager.getLeftDuration(string.format("upcd%d", bid))
  --end
  print("UpgradeManager.setUpgradeCoolDown", sc.upgradeCooldown)
  local lb = sc.instance.node:getChildByTag(1006)
  local b = sc.instance.node:getChildByTag(1020)
  if sc.upgradeCooldown<=0 then
    TypeCast(lb, "CCLabelTTF"):setString("等待升级")
    --TypeCast(b, "CCButton"):setEnabled(true)
    TypeCast(b, "CCButton"):setTitle("升级")
  else
    TypeCast(lb, "CCLabelTTF"):setString(formatSecond(sc.upgradeCooldown))
    TypeCast(b, "CCButton"):setTitle("加速")
    --TypeCast(b, "CCButton"):setEnabled(false)
    --UpgradeManager.upgradeBuilds:append(bid)
    --if UpgradeManager.upgradeTimerID==nil then
    --  UpgradeManager.upgradeTimerID = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(onUpgradeTimer, 1, false)
    --end
  end
end

function onUpgradeTimer()
  --print("onUpgradeTimer", UpgradeManager.upgradeBuilds:size())
  --local tmpBids = Array:create()
  local isStopTimer = true 
  --for i=0,UpgradeManager.upgradeBuilds:size()-1 do
  for k,v in pairs(UpgradeManager.upgradeBuilds) do
    local bid = k--UpgradeManager.upgradeBuilds:at(i)
    local sc = UpgradeManager.buildingScenes[bid]
    print("onUpgradeTimer", bid, sc, sc.upgradeCooldown) 
    sc.upgradeCooldown = sc.upgradeCooldown-1
    if sc.instance~=nil then
      UpgradeManager.setUpgradeCoolDown(bid)
    end
    if sc.upgradeCooldown>0 then
      isStopTimer = false
      --tmpBids:append(bid)
    end
  end 
  --UpgradeManager.upgradeBuilds = tmpBids
  --if UpgradeManager.upgradeBuilds:size()==0 then
  if isStopTimer==true then
    CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(UpgradeManager.upgradeTimerID)
    print("UpgradeManager.upgradeBuilding stop timer")
    --UpgradeManager.hasTimer = false
    UpgradeManager.upgradeTimerID = nil
  end 
end
